<template>
	<div class="transfer_tree_box">
		<slot name="treeRoot"></slot>
		<!-- ant-design-vue tree -->
		<a-ztree
			class="a-tree-custom-style"
			v-if="newTreeData.length > 0"
			:checkedKeys="checkedKeys"
			:show-line="showLine"
			:checkable="checkable"
			:selected-keys="selectedKeys"
			:expanded-keys="expandedKeys"
			:showIcon="true"
			:check-strictly="true"
			:tree-data="newTreeData"
			:replaceFields="replaceFields"
			:icon="renderIcon"
			@select="onSelect"
			@check="onCheck"
		>
		</a-ztree>
	</div>
</template>

<script>
import { EntityTypes } from '@/utils/orgConstants.js';
import { TreeUtils } from '@/utils/treeUtils';
import seletOrgIconConfig from '../../config/iconfont';

export default {
	name: 'transferTree',
	components: {},
	props: {
		checkable: { type: Boolean, default: false },
		checkStrictly: { type: Boolean, default: false },
		checkedList: { type: Array, default: () => [] },
		defaultSelected: { type: Array, default: () => [] },
		treeData: { type: Array, default: () => [] },
		defaultExpandedKeys: { type: Array, default: () => [] },
		isUseDefaultIcon: { type: Boolean, default: true },
		iconType: {type: String, default: '' },
		isCustomIcon: { type: Boolean, default: false },
		noCheckId: [String, Number]
	},
	data() {
		return {
			selectedKeys: this.defaultSelected,
			expandedKeys: [],
			replaceFields: {
				title: 'name',
				// children: 'children',
				key: 'id'
				// disabled: false
			},
			snapNode: '',
			visible: false,
			loading: false,

			checkedData: this.checkedList
		};
	},
	computed: {
		checkedKeys: {
			get() {
				return this.checkedData;
			}
		},
		newTreeData() {
			let data = TreeUtils.resetTreeData(this.treeData);
			return data;
		}
	},
	watch: {
		defaultExpandedKeys(val) {
			this.expandedKeys = val.slice(0);
		},
		defaultSelected(val) {
			this.selectedKeys = val;
		},
		checkedList(val) {
			this.checkedData = val;
		}
	},
	updated() {
		if (this.defaultSelected.length) {
			this.$emit('toScrollIntoSelectedNode', this.defaultSelected[0]);
		}
	},
	mounted() {
	},
	methods: {
		onCheck(keys, callbackData) {
			// 删除未验证的节点id
			this.checkedData = keys;
			let nodeData = callbackData.node.data;
			let index = this.checkedKeys.findIndex(el => el === nodeData.id);
			if (index !== -1) {
				this.checkedData.splice(index, 1);
			}
			let node = {...nodeData, checked: !callbackData.checked};
			let checkeds = [nodeData];
			let argments = {
				checkeds: checkeds,
				ids: JSON.parse(JSON.stringify(keys))
			};
			this.$emit('toCheckedNode', argments, node);
		},
		onSelect(keys, selectData) {
			let data = selectData.node.data;
			let id = data.id;
			if (id.length === 0) {
				// 不能取消选择
				return;
			}
			this.selectedKeys = [id];
			let selected = [data];
			this.$emit('toSelectNode', { selected: selected, id: id });
		},
		onHandleCancel() {
			this.visible = false;
		},
		renderIcon(treeNode) {
			let { data } = treeNode;
			let type = data.type;
			// 单位icon
			let accountIcon = seletOrgIconConfig.seletOrgIconPath['cap-icon-alternative-company'];
			// 部门icon
			let departmentIcon = seletOrgIconConfig.seletOrgIconPath['cap-icon-alternative-department'];
			// 缺省icon
			let defaultIcon = seletOrgIconConfig.seletOrgIconPath['cap-icon-alternative-default'];
			// 外部单位icon
			let externalIcon = seletOrgIconConfig.seletOrgIconPath['cap-icon-alternative-external-un'];

			if (this.isUseDefaultIcon) {
				if (type.indexOf(EntityTypes.Account) !== -1) {
					return accountIcon;
				} else {
					return departmentIcon;
				}
			}

			if (!this.isUseDefaultIcon && this.isCustomIcon) {
				if (data.parentId == -1) {
					return accountIcon;
				} else {
					return defaultIcon;
				}
			} else {
				return externalIcon;
			}
		}
	}
};
</script>

<style scoped lang="scss">
.transfer_tree_box {
	background: #fff;
	overflow: auto;
	height: 100%;
	padding: 6px 0 0 8px;
	box-sizing: border-box;
	.tree-node-line-box {
		.tree-node-line-box-icon {
			display: flex;
			align-items: center;
			justify-content: center
		}
	}
	svg use{ pointer-events: none; }
}
</style>
